package com.sbtr.business.user.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.sbtr.util.PageDtoUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbtr.common.PageDto;
import com.sbtr.open.user.dto.UserAllowCountDto;
import com.sbtr.business.user.entity.UserAllowCount;
import com.sbtr.business.user.mapper.UserAllowCountMapper;
import com.sbtr.business.user.service.IUserAllowCountService;
import com.sbtr.util.BeanCopyUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author oujh5

 */
@Transactional
@Service
public class UserAllowCountServiceImpl extends ServiceImpl<UserAllowCountMapper, UserAllowCount> implements IUserAllowCountService {

	@Override
 	public List<UserAllowCountDto> searchByUserIds(List<String> userIds){
		
		QueryWrapper<UserAllowCount> wrapper = new QueryWrapper<>();
		wrapper.in("user_id", userIds);
		List<UserAllowCount> list = this.list(wrapper);
		if(list != null && !list.isEmpty()){
			return BeanCopyUtils.copyProperties(list, UserAllowCountDto.class);
		}
		return null;
	}
	
	@Override
 	public PageDto<UserAllowCountDto> search(String userId,Long currPage, Long pageSize){
		
		IPage<UserAllowCount> page = new Page<>();
		if(currPage != null){
			page.setCurrent(currPage);
		}
		if(pageSize != null){
			page.setSize(pageSize);
		}
		
		QueryWrapper<UserAllowCount> wrapper = new QueryWrapper<>();
		if(StringUtils.isNotBlank(userId)){
			wrapper.eq("user_id", userId);
		}
		wrapper.orderByDesc("id");
		page = page(page, wrapper);
		
		return PageDtoUtils.getPageDtoByIPage(page, UserAllowCountDto.class);
	}
	
	@Override
 	public boolean batchDelete(List<String> userIds){
		List<UserAllowCountDto> list = this.searchByUserIds(userIds);
		Assert.isTrue(list != null && !list.isEmpty(), "数据不存在");
		QueryWrapper<UserAllowCount> wrapper = new QueryWrapper<>();
		wrapper.in("user_id", userIds);
		return this.remove(wrapper);
	}
	
	@Override
 	public boolean batchSave(List<UserAllowCountDto> dtoList){
		List<String> userIds = new ArrayList<>();
		dtoList.forEach(d -> userIds.add(d.getUserId()));
		QueryWrapper<UserAllowCount> wrapper = new QueryWrapper<>();
		wrapper.in("user_id", userIds);
		this.remove(wrapper);
		return this.saveBatch(BeanCopyUtils.copyProperties(dtoList, UserAllowCount.class));
	}
	
}
